package com.npjscg.medicalpro.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.common.Result;
import com.npjscg.medicalpro.config.AuthAccess;
import com.npjscg.medicalpro.entity.Dep;
import com.npjscg.medicalpro.entity.User;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.service.IDepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author raote
 * @since 2022-06-25
 */
@Api(tags = "部门管理模块")
@RestController
@RequestMapping("/dep")
public class DepController {

    @Autowired
    private IDepService depService;

    //注册新部门
    @ApiOperation(value = "注册新部门")
    @PostMapping("/newDep")
    public Result newDep(@RequestBody Dep dep){
        String depname = dep.getDepname();
        Integer  fid= dep.getfId();
        if (StrUtil.isBlank(depname) || fid == null)
            return Result.error(Constants.CODE_400, "部门没有名称，父亲部门选择出错！");
        return Result.success(depService.newDep(dep));
    }


    //通过父id来获得部门 ，级联选择器用
    @AuthAccess
    @ApiOperation(value = "通过父亲id来获得部门")
    @GetMapping("/getDepByfather/{f_id}")
    public Result getDepByfather(@PathVariable int f_id){

        if(f_id < 0)
            return Result.error(Constants.CODE_400,"提供查找id错误");

        return Result.success(depService.getDepByfather(f_id));
    }


    //更新部门信息 仅包括部门名字与部门代码
    @ApiOperation(value = "更新部门信息")
    @GetMapping("/update/{newname}/{newdepcode}/{id}")
    public Result updatename(@PathVariable String newname, @PathVariable String newdepcode,@PathVariable int id){
        if (StrUtil.isBlank(newname) || StrUtil.isBlank(newdepcode))
            return Result.error(Constants.CODE_400, "新部门名和新部门代码不能为空或空格！");
        return Result.success(depService.updatename(newname,newdepcode, id));
    }


    //分页查询
    //  查询所有部门：
    //需要传入当前页号、当前页面需要多少条记录，可选部门名字进行模糊查询
    @ApiOperation(value = "部门分页查询")
    @GetMapping(value = {"/findAll/{pagenum}/{pagesize}/{depname}", "/findAll/{pagenum}/{pagesize}"})
    public Result findAll(@PathVariable int pagenum, @PathVariable int pagesize, @PathVariable(required = false) String depname){
        //设置起始值及每页条数
        if (depname == null)
            depname = "";
        Page<Dep> page = new Page<>(pagenum,pagesize);
        return Result.success(depService.selectPage(page, depname));
    }

    //删除部门
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/delete/{id}")
    public Result deleteDep(@PathVariable int id){
        if (id < 0 )
            return Result.error(Constants.CODE_400,"id提供错误");

        depService.deleteDep(id);
        return Result.success();
    }


    @ApiOperation(value = "通过部门代码来获取部门")
    @GetMapping("/getByCode/{depcode}")
    public Result getByCode(@PathVariable String depcode){
        if (depcode == null)
            return Result.error(Constants.CODE_600, "部门代码为空");
        else
            return Result.success(depService.getByCode(depcode));
    }





}
